import logging
from mysql.connector import pooling
from dotenv import load_dotenv
import os
from datetime import datetime
from dataclasses import dataclass
from typing import Optional

# 加载.env文件
load_dotenv()

host = os.getenv('MYSQL_HOST')
user = os.getenv('MYSQL_USER')
password = os.getenv('MYSQL_PASSWORD')
db = os.getenv('MYSQL_DB')

# 创建一个连接池
dbconfig = {
    "host": host,
    "user": user,
    "password": password,
    "database": db
}
pool = pooling.MySQLConnectionPool(pool_name="my_pool", pool_size=10, **dbconfig)

logging.basicConfig(filename='../logs/aiBase.log',
                    level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(message)s')

conn = pool.get_connection()
cursor = conn.cursor()


@dataclass
class AiBase:
    Name: str
    Summary: str
    Audience: str
    ScenarioDemo: str
    Features: str
    Label: str
    Url: str
    CreateTime: float
    Id: Optional[int] = None


# 获取列表
@staticmethod
def getList():
    sql = "SELECT * FROM aibase"
    cursor.execute(sql)
    result = cursor.fetchall()
    return result


# 新增
def add(aiBase: AiBase):
    sql = "INSERT INTO aibase(name,summary,audience,scenario_demo,features,label,url,create_time) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"
    cursor.execute(sql, (aiBase.Name, aiBase.Summary, aiBase.Audience, aiBase.ScenarioDemo, aiBase.Features, aiBase.Label, aiBase.Url, aiBase.CreateTime))
    conn.commit()
